import sys

sys.path.append("..")
from lib.downloadImage import downloadImage
from lib.Spider import Spider



class DeskZol(Spider):

    def getImageAll(self):
        return self.bs4.select("li > a.pic > img")

    @Spider.Decorators.src
    def getImageAllSrc(self):
        return self.getImageAll()

    @Spider.Decorators.attr("alt")
    def getImageAllAlt(self):
        return self.getImageAll()

    def downloadAll(self):
        alts = self.getImageAllAlt()
        srcs = self.getImageAllSrc()
        for i in range(len(srcs)):
            downloadImage(srcs[i],alts[i])

    @staticmethod
    def generator(start: int, end: int):
        for i in range(start, end):
            yield DeskZol(f"https://desk.zol.com.cn/17/{i}.html")


if __name__ == "__main__":
    # 下载第 5-10 页的图片到 resource\image 目录下
    for spider in DeskZol.generator(5, 10):
        spider.downloadAll()
